package com.gitee.qdbp.general.mgr.controller.basedata.record;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.utils.EnumTools;
import com.gitee.qdbp.base.utils.SessionTools;
import com.gitee.qdbp.general.common.api.oplog.model.OperateRecordBean;
import com.gitee.qdbp.general.common.api.oplog.model.OperateRecordWhere;
import com.gitee.qdbp.general.common.api.oplog.service.IOperateRecordExecutor;
import com.gitee.qdbp.general.common.api.oplog.service.IOperateRecordQueryer;
import com.gitee.qdbp.general.common.enums.LogProcessState;
import com.gitee.qdbp.general.common.enums.LogRequestType;
import com.gitee.qdbp.general.common.enums.LogResultType;
import com.gitee.qdbp.tools.utils.ConvertTools;

/**
 * 操作日志基本信息控制器
 *
 * @author zhaohuihua
 * @version 170401
 */
@Controller
@RequestMapping("actions/operate/record")
public class OperateRecordController {

    @Autowired
    private IOperateRecordQueryer operateRecordQueryer;
    @Autowired
    private IOperateRecordExecutor operateRecordExecutor;

    /**
     * 查询操作日志列表
     *
     * @param paging 分页信息
     * @param model 查询条件
     * @return 操作日志列表
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions("operate:record:query")
    public ResponseMessage list(OperateRecordWhere model, OrderPaging paging, Boolean extra) {
        try {
            PageList<OperateRecordBean> list = operateRecordQueryer.list(model, paging);
            ResponseMessage result = new ResponseMessage(list);
            if (Boolean.TRUE.equals(extra)) {
                result.addExtra(EnumTools.getEnumsResource(LogRequestType.class));
                result.addExtra(EnumTools.getEnumsResource(LogResultType.class));
                result.addExtra(EnumTools.getEnumsResource(LogProcessState.class));
            }
            return result;
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /**
     * 标记处理状态
     * 
     * @param id
     * @param state
     * @param desc
     * @return
     */
    @ResponseBody
    @RequestMapping("mark")
    @RequiresPermissions("operate:record:mark")
    public ResponseMessage mark(String[] ids, LogProcessState state, String desc) {
        try {
            IAccount me = SessionTools.getLoginUser();
            operateRecordExecutor.mark(ConvertTools.toList(ids), state, desc, me);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }
}
